package com.whenjun.service.impl;

import com.ifootbook.util.CopyerObject;
import com.ifootbook.util.EmptyUtil;
import com.ifootbook.util.FootBookException;
import com.whenjun.dto.base.BaseId;
import com.whenjun.dto.kq.KqDepartmentAddReq;
import com.whenjun.dto.kq.KqDepartmentDTO;
import com.whenjun.dto.kq.KqDepartmentReq;
import com.whenjun.dto.kq.KqDepartmentUpdReq;
import com.whenjun.entity.KqDepartment;
import com.whenjun.mapper.KqDepartmentMapper;
import com.whenjun.service.IKqDepartmentGroupService;
import com.whenjun.service.IKqDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whenjun.util.SaTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whenjun
 * @since 2024-02-22
 */
@Service
public class KqDepartmentServiceImpl extends ServiceImpl<KqDepartmentMapper, KqDepartment> implements IKqDepartmentService {

    @Autowired
    private KqDepartmentGroupServiceImpl KqDepartmentGroupService;


    @Transactional
    public List<KqDepartmentDTO> KqDepartments(KqDepartmentReq kqDepartmentReq) {
        List<KqDepartment> kqDepartments = this.lambdaQuery()
                .like(!EmptyUtil.isEmpty(kqDepartmentReq.getName()), KqDepartment::getName, kqDepartmentReq.getName())
                .eq(KqDepartment::getState, false)
                .list();

        List<KqDepartmentDTO> kqDepartmentDTOS = CopyerObject.copyerListObject(kqDepartments, KqDepartmentDTO.class);

        return kqDepartmentDTOS;
    }

    @Transactional
    public void KqDepartmentAdd(KqDepartmentAddReq kqDepartmentAddReq) {
        KqDepartment kqDepartment = this.lambdaQuery()
                .eq(KqDepartment::getName, kqDepartmentAddReq.getName())
                .eq(KqDepartment::getState, false)
                .one();

        if (kqDepartment != null) {
            throw new FootBookException("name已存在，请修改后再试");
        }
        LocalDateTime now = LocalDateTime.now();
        String userId = SaTokenUtil.getUserId();

        kqDepartment = new KqDepartment();

        kqDepartment.setCreateTime(now);
        kqDepartment.setCreateUser(userId);
        kqDepartment.setState(false);
        kqDepartment.setName(kqDepartmentAddReq.getName());

        this.save(kqDepartment);
    }

    @Transactional
    public void KqDepartmentDel(BaseId baseId) {
        KqDepartment kqDepartment = this.getById(baseId.getId());

        if (kqDepartment == null || kqDepartment.getState().equals(true)) {
            throw new FootBookException("id错误");
        }
        LocalDateTime now = LocalDateTime.now();
        String userId = SaTokenUtil.getUserId();

        kqDepartment.setUpdateTime(now);
        kqDepartment.setUpdateUser(userId);
        kqDepartment.setState(true);

        // 删除所属班组以及班组所属人群   全部物理删除
//        this.updateById(kqDepartment);
        this.removeById(kqDepartment);
        // 同样也是物理删除
        KqDepartmentGroupService.KqGroupDelByDepartmentId(kqDepartment.getId());
    }

    @Transactional
    public void KqDepartmentUpd(KqDepartmentUpdReq kqDepartmentUpdReq) {
        KqDepartment kqDepartment = this.getById(kqDepartmentUpdReq.getId());
        if (kqDepartment == null || kqDepartment.getState().equals(true)) {
            throw new FootBookException("id错误");
        }

        KqDepartment kqDepartmentCheck = this.lambdaQuery()
                .eq(KqDepartment::getName, kqDepartmentUpdReq.getName())
                .eq(KqDepartment::getState, false)
                .one();

        if (kqDepartmentCheck != null) {
            throw new FootBookException("name已存在，请修改后再试");
        }

        LocalDateTime now = LocalDateTime.now();
        String userId = SaTokenUtil.getUserId();

        kqDepartment.setUpdateTime(now);
        kqDepartment.setUpdateUser(userId);
        kqDepartment.setName(kqDepartmentUpdReq.getName());

        this.updateById(kqDepartment);
    }
}
